|
|
|
|
|
|
|
The pictures shown in Figures 12.23 through 12.25 contain only a few objects, so using quadtrees is overkill. Drawing and searching through all the objects one at a time is fast enough for most purposes. On the other hand, a more complex picture might contain hundreds or thousands of objects. In that case, the savings you get by using a quadtree can be tremendous. |
|
|
|
|
|
|
|
|
Example program Hexes2 is similar to program Hexes1 except it uses a quadtree to display and locate hexagons more quickly. In addition to displaying the hexagons, this program draws red lines around each of the quadtree nodes so that you can see them. |
|
|
|
|
|
|
|
|
Although this program takes about as long as program Hexes1 to display the complete picture, it is much faster when you zoom in closely. If you zoom in as far as possible on a 133MHz Pentium, Hexes1 needs more than two seconds to display the part of the picture that is visible. Hexes2 takes about half the time. |
|
|
|
|
|
|
|
|
The quadtree gives an even more dramatic improvement when the program must locate an object. The time needed by program Hexes1 to locate a hexagon depends on where the hexagon is located in the program's list of objects. If the hexagon is near the end of the list, this program takes more than a second to locate it on a 133MHz Pentium. Program Hexes2 can locate any hexagon almost instantly. |
|
|
|
|
|
|
|
|
Programs Hexes1 and Hexes2 have one other feature worth noting. If you zoom in on an area that is too small, Visual Basic is unable to draw all of the picture. When it tries to scale the world coordinates to fit in the viewport, some of the points that fall far outside the world window fall very far outside the viewport. In fact, these points fall so far outside that their coordinates overflow the values allowed for the viewport PictureBox, and Visual Basic generates an overflow error. To prevent this, these programs do not allow the user to zoom in too far. |
|
|
|
|
|
|
|
|
The most important concept described in this chapter is the use of homogeneous coordinates. Using homogeneous coordinates, you can represent translation, scaling, reflection, and rotation uniformly with matrices. You can combine a complex series of transformations into a single matrix which you can then apply quickly and easily to the points in your objects. |
|
|
|
|
|
|
|
|
Understanding homogeneous coordinates in two dimensions is particularly important since later chapters build on these concepts. Chapter 13, Three-Dimensional Transformations, extends the ideas presented here to manipulate three-dimensional objects. If you have a good understanding of the two-dimensional concepts, you will be able to build programs that draw three-dimensional scenes with little trouble. |
|
|
|
|
|